package com.lei.lucene;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * 搜索索引
 * @author Spinacia
 *
 */
public class SearchIndex {
	
	public IndexSearcher getIndexSearcher() throws Exception {
		//打开索引库
		Directory directory = FSDirectory.open(new File("F:\\indexs"));
		//得到一个索引读对象(indexreader
		IndexReader indexReader = DirectoryReader.open(directory);
		//得到一个索引搜索对象(indexsearcher
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		return indexSearcher;
	}
	
	private void printResult(IndexSearcher indexSearcher, Query query)
			throws IOException {
		TopDocs topDocs = indexSearcher.search(query , 10);
		System.out.println("查询到的总记录数:" + topDocs.totalHits);
		ScoreDoc[] scoreDocs = topDocs.scoreDocs;
		for (ScoreDoc scoreDoc : scoreDocs) {
			Integer docIdx = scoreDoc.doc;
			Document document = indexSearcher.doc(docIdx);
			System.out.println(document.get("filename"));
			System.out.println(document.get("filesize"));
			System.out.println(document.get("content"));
			System.out.println(document.get("filepath"));
		}
		//释放资源
		try {
			indexSearcher.getIndexReader().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//查询全部文档
	@Test
	public void matchAllDocsQuery() throws Exception {
		
		//查询
		Query query = new MatchAllDocsQuery();
		
		printResult(this.getIndexSearcher(), query);
	}
	
	//数值排序查询
	@Test
	public void numericRangeQuery() throws Exception {
		
		//查询
		Query query = NumericRangeQuery.newLongRange("filesize", 100L, 1000L, true, true);
		
		printResult(this.getIndexSearcher(), query);
	}
	
	//多条件查询
	@Test
	public void booleanQuery() throws Exception {
		
		//查询
		BooleanQuery query = new BooleanQuery();
		//查询1
		Query query1 = NumericRangeQuery.newLongRange("filesize", 100L, 1000L, false, true);
		//查询2
		Query query2 = new TermQuery(new Term("filename", "apache"));
		query.add(query1, Occur.SHOULD);
		query.add(query2, Occur.MUST);
		System.out.println(query);
		
		printResult(this.getIndexSearcher(), query);
	}
	
	//使用querypaser查询
	@Test
	public void queryParser() throws Exception {
		
		QueryParser queryParser = new QueryParser("filename", new IKAnalyzer());
		Query query = queryParser.parse("filename:apache");
		System.out.println(query);
		
		this.printResult(getIndexSearcher(), query);
	}
	
	//使用multiquerypaser查询
	@Test
	public void multiQueryParser() throws Exception {
		
		String[] fields = new String[]{"filename","content"};
		QueryParser queryParser = new MultiFieldQueryParser(fields, new IKAnalyzer());
		Query query = queryParser.parse("+filename:spring +content:spring");
		System.out.println(query);
		
		this.printResult(getIndexSearcher(), query);
	}

}
